/**
 * gwt-validator 
 *
 * Copyright (C) 2006  Nikolaus Rumm, Christian Simonutti
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package gwt.validator.client.check;

import gwt.validator.client.exception.ValidationIssue;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBoxBase;

/**
 * Validator for checking that two Strings are equal.
 * 
 * @author <a href="mailto:nikolaus.rumm@gmail.com">Nikolaus Rumm</a>
 */
public class StringEqualCheck extends BaseCheck {

	/**
	 * The text box that displays the text to be validated
	 */
	private TextBoxBase textBox;

	/**
	 * The label that names the text box whose data must be equal
	 */
	private Label labelEqual;

	/**
	 * The name of the text box whose data must be equal
	 */
	private String propertyNameEqual;

	/**
	 * The text box that displays the text that must be equal
	 */
	private TextBoxBase textBoxEqual;

	/**
	 * Constructor
	 * 
	 * @param aTextBox
	 *            The text box that displays the text to be validated
	 * @param aTextBoxEqual
	 *            The text box whose data must be equal
	 * @param aLabel
	 *            The label that displays the property's name
	 * @param aLabelEqual
	 *            The label that names the text box whose data must be equal
	 */
	public StringEqualCheck(TextBoxBase aTextBox, TextBoxBase aTextBoxEqual,
			Label aLabel, Label aLabelEqual) {
		super(aLabel);

		checkNotNull(aTextBox);
		checkNotNull(aTextBoxEqual);
		checkNotNull(aLabelEqual);

		this.textBox = aTextBox;
		this.labelEqual = aLabelEqual;
		this.textBoxEqual = aTextBoxEqual;
	}

	/**
	 * Constructor
	 * 
	 * @param aTextBox
	 *            The text box that displays the text to be validated
	 * @param aTextBoxEqual
	 *            The text box whose data must be equal
	 * @param aPropertyName
	 *            The property's name
	 * @param aPropertyNameEqual
	 *            The equal property's name
	 */
	public StringEqualCheck(TextBoxBase aTextBox, TextBoxBase aTextBoxEqual,
			String aPropertyName, String aPropertyNameEqual, int aMinLength) {
		super(aPropertyName);

		checkNotNull(aTextBox);
		checkNotNull(aTextBoxEqual);
		checkNotNull(aPropertyNameEqual);

		this.textBox = aTextBox;
		this.textBoxEqual = aTextBoxEqual;
		this.propertyNameEqual = aPropertyNameEqual;
	}

	public Label getLabelEqual() {
		return labelEqual;
	}

	public TextBoxBase getTextBoxEqual() {
		return textBoxEqual;
	}

	protected String getPropertyNameEqual() {
		String res = null;
		if (this.propertyNameEqual != null) {
			res = this.propertyNameEqual;
		} else if (this.labelEqual != null) {
			res = this.labelEqual.getText();
		} else {
			res = "field";
		}
		return res;
	}

	public List validateImpl() {
		List res = new ArrayList();

		String txt = this.textBox.getText();
		String equalTxt = this.textBoxEqual.getText();
		if (!txt.equals(equalTxt)) {
			res.add(new ValidationIssue(this.getIssueMessageFactory()
					.stringNotEqual(this.getPropertyName(),
							this.getPropertyNameEqual())));
		}

		return res;
	}
}
